﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class ApplicationDetail_Client : System.Web.UI.Page
    {
        public ApplicationForm application;
        public bool recieveLnkVisiable = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindApplication();
                InitialLink();
            }
        }

        private void InitialLink()
        {
            viewHyperLink.NavigateUrl = Request.Url.ToString().Replace("ApplicationDetail.aspx", "OriginalApplicationDetail.aspx") + "&RType=1";

            if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.Customer)
            {
                returnHyperLink.NavigateUrl = "MyApplication.aspx";
            }
            else
            {
                returnHyperLink.NavigateUrl = "AllOnProcessApplication.aspx";
            }

            if (!string.IsNullOrEmpty(Request.QueryString["BackType"]))
            {
                returnHyperLink.NavigateUrl = "AdminHome.aspx";
            }

            receiveHyperLink.NavigateUrl = string.Format("GiveFeedback.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
        }

        private void BindApplication()
        {
            ApplicationService service = new ApplicationService();
            application = service.GetApplicationFormDetailByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            RMACodeLabel.Text = application.ApplicationCode;
            statusLabel.Text = application.Status;
            runningNumberLabel.Text = application.RunningNumber;
            companyLabel.Text = application.User.FullName;
            applyTimeLabel.Text = application.ApplicationDateTime == null ? "" : Convert.ToDateTime(application.ApplicationDateTime).ToString("yyyy-MM-dd");
            receiveTimeLabel.Text = application.ReceiveDateTime == null ? "" : Convert.ToDateTime(application.ReceiveDateTime).ToString("yyyy-MM-dd");
            addressLabel.Text = application.User.UserAddress;
            completeTimeLabel.Text = application.CompleteDateTime == null ? "" : Convert.ToDateTime(application.CompleteDateTime).ToString("yyyy-MM-dd");
            sendTimeLabel.Text = application.SendDateTime == null ? "" : Convert.ToDateTime(application.SendDateTime).ToString("yyyy-MM-dd");
            phoneNumberLabel.Text = application.User.UserPhone;
            faxLabel.Text = application.User.UserFax;
            contactLabel.Text = application.User.ContactPerson;
            emailLabel.Text = application.User.UserEmail;
            cellPhoneLabel.Text = application.User.UserMobilePhone;
            agentLabel.Text = application.ProviderName;

            if (application.ServiceStation != null)
            {
                serviceStationPlaceHolder.Visible = true;
                stationNameLabel.Text = application.ServiceStation.Name;
                stationAddressLabel.Text = application.ServiceStation.Address;
                contactPersonLbl.Text = application.ServiceStation.ContactPerson;
                contactPhoneLbl.Text = application.ServiceStation.Phone;
            }

            List<ApplicationProduct> products = new List<ApplicationProduct>();
            foreach (ApplicationProduct product in application.ApplicationProduct)
            {
                if (!product.ApplicationForm.FeedbackReference.IsLoaded)
                {
                    product.ApplicationForm.FeedbackReference.Load();
                }
                if (product.SendBackTime.HasValue && !product.ClientReceiveDateTime.HasValue)
                {
                    product.Status = "已发货待客户确认";
                }
                else if (product.ClientReceiveDateTime.HasValue)
                {                    
                    if (product.ApplicationForm.Feedback == null)
                    {
                        product.Status = "客户已收货确认";
                    }
                    else
                    {
                        product.Status = "客户已收货确认并反馈";
                    }
                }
                
                if ((application.ApplicationType.Equals("BD") ||
                    application.ApplicationType.Equals("BB"))&&
                    product.BJXXReturnBack.HasValue &&
                    application.Feedback == null)                
                {
                    application.Status = "备件已归还";
                }


                if (!product.QuatationForm.IsLoaded)
                {
                    product.QuatationForm.Load();
                }
                if (product.QuatationForm != null && product.QuatationForm.Count > 0)
                {
                    product.ReasonOfCost = product.QuatationForm.FirstOrDefault().Reason;
                    product.RepairAccessory = string.Empty;
                    foreach(QuatationForm qForm in product.QuatationForm)
                    {
                        if (string.IsNullOrEmpty(product.RepairAccessory))
                        {
                            product.RepairAccessory = qForm.AccessoryName;
                        }
                        else
                        {
                            product.RepairAccessory = "," + qForm.AccessoryName;
                        }
                    }
                }
                else
                {
                    product.ReasonOfCost = "";
                }                

                products.Add(product);
            }

            productGridView.DataSource = products.OrderBy(p => p.ApplicationForm.ApplicationDateTime);
            productGridView.DataBind();

            userLabel.Text = ContextService.CurrentAccount.UserName;

            if (!application.ApplicationPicture.IsLoaded)
            {
                application.ApplicationPicture.Load();
            }
            List<ApplicationPicture> picturelist = application.ApplicationPicture.ToList();
            //绑定图片
            Table table = new Table();
            table.Rows.Add(new TableRow());
            table.Rows.Add(new TableRow());
            foreach (ApplicationPicture picture in picturelist)
            {
                TableCell celllImage = new TableCell();
                Image imageControl = new Image();
                imageControl.Height = 100;
                imageControl.ImageUrl = "Picture/" + picture.PictureNameOnServer;
                imageControl.Attributes.Add("OnClick", "window.open('ImageDetail.aspx?Name=" + picture.PictureNameOnServer + "')");
                celllImage.Controls.Add(imageControl);
                table.Rows[0].Cells.Add(celllImage);

                TableCell cellName = new TableCell();
                Label label = new Label();
                label.Text = picture.PictureName;
                cellName.Controls.Add(label);
                table.Rows[1].Cells.Add(cellName);
            }
            picturePanel.Controls.Add(table);

            if (application.Status.Equals("客户信息反馈"))
            {
                receiveHyperLink.Enabled = false;
            }
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }
    }
}
